package yx.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.message.client.Client;
import yx.model.BsSms;
import yx.model.SmSms;
import yx.service.BsSmsServiceInt;
import yx.service.SmsServiceInt;
import yx.vo.BsSmsVO;
import yx.vo.JsonVO;
import yx.vo.UserVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "bssmsAction")
public class BsSmsAction extends BaseAction implements ModelDriven<BsSmsVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BsSmsAction.class);

	private BsSmsVO sms = new BsSmsVO();

	@Override
	public BsSmsVO getModel() {
		return sms;
	}

	public void querysms() {
		super.writeJson(smsService.datagrid(sms));
	}
    @Autowired
	private BsSmsServiceInt smsService;
	@Autowired
	private SmsServiceInt   configService;
	

	public void addBsSms() {
		JsonVO j = new JsonVO();
		try {
			SmSms config = configService.get();
			Client client = new Client(config.getSdk(), config.getPassword());
			//client最后是拼成xml了，所以要按照xml的语法来转义
			String content = sms.getContext();
			if(content.indexOf("&")>=0) {
				content=content.replace("&","&amp;");
			}
			if(content.indexOf("<")>=0) {
				content=content.replace("<","&lt;");
			}
			if(content.indexOf(">")>=0) {
				content=content.replace(">","&gt;");
			}
			//短信发送		
			String result_mt = client.mt(sms.getPhone(), content, "", "", "");
			if(result_mt.startsWith("-")||result_mt.equals(""))//以负号判断是否发送成功
			{
				sms.setStatus(1);
			}
			else
			{
			    sms.setStatus(0);
			}
			Object obj = ServletActionContext.getRequest().getSession().getAttribute("LOGIN_USER");
			if(obj instanceof UserVO){
				UserVO login = (UserVO)obj;
				sms.setOptId(login.getId());
			}
			BsSmsVO u = smsService.save(sms);
			j.setSuccess(true);
			j.setMsg("保存记录成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	/**
	 * 重发 消息 （这里只能发送失败的消息 ，成功消息不支持重发）
	 */
	public void beSendBssms(){
		List<BsSms> sm = smsService.queryBeSendInfo(sms.getIds());
		JsonVO j = new JsonVO();
		SmSms config = configService.get();
			try {
				Client client = new Client(config.getSdk(), config.getPassword());
				for(BsSms be : sm){
					//client最后是拼成xml了，所以要按照xml的语法来转义
					String content = be.getContext();
					if(content.indexOf("&")>=0) {
						content=content.replace("&","&amp;");
					}
					if(content.indexOf("<")>=0) {
						content=content.replace("<","&lt;");
					}
					if(content.indexOf(">")>=0) {
						content=content.replace(">","&gt;");
					}
					//短信发送		
					String result_mt = client.mt(be.getPhone(), content, "", "", "");
					if(result_mt.startsWith("-")||result_mt.equals(""))//以负号判断是否发送成功
					{
						be.setStatus(1);
					}
					else
					{
						be.setStatus(0);
					}
					BsSmsVO vo = new BsSmsVO();
					vo.setId(be.getId());
					vo.setContext(be.getContext());
					vo.setStatus(be.getStatus());
					smsService.edit(vo);
				}
				j.setSuccess(true);
				j.setMsg("保存重发记录成功！");
			} catch (Exception e) {
				j.setMsg(e.getMessage());
			}
		
		super.writeJson(j);
	}
	
	/**
	 * 编辑
	 */
	public void editsms() {
		BsSms u = smsService.edit(sms);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

	/**
	 * 删除 
	 */
	public void removeBssms() {
		smsService.remove(sms.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	/**
	 * 查询余额
	 */
	public void queryAccount(){
		SmSms config = configService.get();
		String account = "";
		try {
			Client client = new Client(config.getSdk(), config.getPassword());
			account = client.getBalance();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg(account);
		super.writeJson(j);
	}

}
